package com.haohuo.question2;

import java.util.*;

/**
 * note：
 * <p>
 * author: Caozhitong
 **/
public class tuop {
    static class node {
        int value;
        List<Integer> next;

        public node(int value) {
            this.value = value;
            next = new ArrayList<Integer>();
        }

        public void setnext(List<Integer> list) {
            this.next = list;
        }
    }

    public static void main(String[] args) {
        node[] nodes = new node[8];//储存节点
        int a[] = new int[8];//储存入度
        List<Integer> list[] = new ArrayList[8];//临时空间，为了存储指向的集合
        for (int i = 1; i < 8; i++) {
            nodes[i] = new node(i);
            list[i] = new ArrayList<Integer>();
        }
        initmap(nodes, list, a);
        Stack<node> s1 = new Stack<node>();
        for (int i = 1; i < 7; i++) {
            if (a[i] == 0) {
                s1.add(nodes[i]);
            }

        }
        Map<Integer, String> kv = new HashMap();
        kv.put(1, "A");
        kv.put(2, "B");
        kv.put(3, "C");
        kv.put(4, "D");
        kv.put(5, "E");
        kv.put(6, "F");
        kv.put(7, "G");
        while (!s1.isEmpty()) {
            node n1 = s1.pop();//抛出输出

            System.out.print(kv.get(n1.value) + " ");

            List<Integer> next = n1.next;
            for (int i = 0; i < next.size(); i++) {
                a[next.get(i)]--;//入度减一
                if (a[next.get(i)] == 0)//如果入度为0
                {
                    s1.add(nodes[next.get(i)]);
                }
            }
        }
    }

    private static void initmap(node[] nodes, List<Integer>[] list, int[] a) {
        list[1].add(2);
        list[1].add(3);
        nodes[1].setnext(list[1]);
        a[2]++;
        a[3]++;
        list[2].add(4);
        nodes[2].setnext(list[2]);
        a[4]++;
        list[3].add(6);
        nodes[3].setnext(list[3]);
        a[6]++;
        list[4].add(5);
        nodes[4].setnext(list[4]);
        a[5]++;
        a[7]++;
        list[5].add(6);
        nodes[5].setnext(list[5]);
        a[6]++;
        list[6].add(7);
        nodes[6].setnext(list[6]);

    }


}
